package com.aaa.service.impl;

import com.aaa.dao.EmpMapper;
import com.aaa.entity.Dept;
import com.aaa.entity.Emp;
import com.aaa.service.EmpService;
import com.aaa.vo.EmpVo;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: work713
 * @description:
 * @author: 李梦朝
 * @create: 2024-07-15 10:50
 **/
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Override
    public R delete(Integer empno) {
        int i = empMapper.deleteById(empno);
        if (i > 0){
            return new R(200,"删除成功",null);
        }else {
            return new R(500,"删除失败",null);
        }
    }

    @Override
    public R insert(Emp emp) {
        int i = empMapper.insert(emp);
        if (i > 0){
            return new R(200,"添加成功",null);
        }else {
            return new R(500,"添加失败",null);
        }
    }

    @Override
    public R selectById(Integer empno) {
        Emp emp1 = empMapper.selectById(empno);
        if (emp1 != null){
            return new R(200,"查询成功",emp1);
        }else {
            return new R(500,"查询失败",null);
        }
    }

    @Override
    public R update(Emp emp) {
        int i = empMapper.updateById(emp);
        if (i > 0){
            return new R(200,"修改成功",null);
        }else {
            return new R(500,"修改失败",null);
        }
    }

    @Override
    public R selectByPage(EmpVo empVo) {
        IPage<Emp> page = new Page<>(empVo.getCurrent(), empVo.getPageSize());
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        //指定查询的列
        if (StringUtils.isNotBlank(empVo.getEname())) {
            wrapper.like("e_name", empVo.getEname());
        }
        if (empVo.getEmpno() != null) {
            wrapper.eq("empno", empVo.getEmpno());
        }
        IPage<Dept> deptIPage = empMapper.selectByPage(page, wrapper);

        return new R(200, "查询成功", deptIPage);
    }
}
